﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class UILayer : MonoBehaviour {
	public GameObject inputText;
	public GameObject searchButton;
	public GameObject gameControl;

	public GameObject filmContent;
	public List<GameObject> filmUI;
	public GameObject preFabFilm;

	public GameObject sessionMovieName;
	public GameObject sessionText;
	int nowSessionNumber;
	public int nowCinemaNumber = 0;

	public GameObject seatX;
	public GameObject seatY;
	public GameObject seatChosen;

	public GameObject orderInfo;


	DomainLayer domainLayer;
	// Use this for initialization
	void Start () {
		domainLayer = gameControl.GetComponents<DomainLayer>()[0];
		foreach (GameObject o in filmUI)
			o.SetActive (false);
	}
	
	// Update is called once per frame
	void Update () {
		if (searchButton.activeSelf == true) {
			if (Input.GetKeyDown (KeyCode.Return))
				OnSearchButtonClick ();
		}
	}

	public void OnSearchButtonClick() {
		string keyWord = inputText.GetComponents<Text>()[0].text;
		List<FilmItem> results = domainLayer.films.searchFilm(keyWord);
		foreach (GameObject o in filmUI)
			o.SetActive (false);
		for(int i = 0; i < results.Count; ++i) {
			filmUI[i].SetActive (true);
			filmUI[i].transform.GetChild (0).GetComponents<Text>()[0].text = results[i].name + "\n" + results[i].actor + "\n"+ results[i].description + "\n" ;
			filmUI[i].GetComponents<FilmData> () [0].filmNumber = results [i].number;
			//GameObject prefabInstance = Instantiate (preFabFilm);
			//prefabInstance.transform.SetParent (filmContent.transform, true);
			//prefabInstance.transform.parent = filmContent.transform;
			//prefabInstance.transform.localPosition = prefabInstance.transform.position;
			//prefabInstance.transform.localRotation = prefabInstance.transform.rotation;
			//prefabInstance.transform.localScale = prefabInstance.transform.localScale;
			//prefabInstance.transform.GetChild (0).GetComponents<Text>()[0].text = x.actor + x.description + x.director;
		}
	}

	public void OnAddSeatsClick() {
		Session curr = domainLayer.cinemas [nowCinemaNumber].getSessionById (nowSessionNumber);
		int row = int.Parse(seatX.GetComponents<Text> () [0].text);
		int column = int.Parse(seatY.GetComponents<Text> () [0].text);
		curr.chooseSeat (row, column);
		seatChosen.GetComponents<Text>()[0].text += row +" 行  " + column + " 列" +"\n";
	}

	public void OnSessionSelected() {
		GameObject.Find ("BackGround/SelectSession").SetActive (false);
		seatChosen.GetComponents<Text> () [0].text = "";
		transform.Find ("BackGround/SelectSeat").gameObject.SetActive (true);
	}

	public void OnFilmSelected(int filmNumber) {
		List<Session> sessionToBeSelected = domainLayer.cinemas[nowCinemaNumber].getSession (filmNumber);
		Debug.Log (sessionToBeSelected.Count);
		sessionMovieName.GetComponents<Text>()[0].text = domainLayer.films.getFilmByNumber (filmNumber).name;
		sessionText.GetComponents<Text> () [0].text = sessionToBeSelected [0].id + "\n" + sessionToBeSelected [0].date + "\n" + sessionToBeSelected [0].time + "\n" + sessionToBeSelected [0].hall;
		GameObject.Find ("SearchMovie").SetActive (false);
		transform.Find ("BackGround/SelectSession").gameObject.SetActive (true);
		nowSessionNumber = sessionToBeSelected [0].id;
	}

	public void createOrder() {
		string info = "订单详情：\n\n";
		info += domainLayer.cinemas[nowCinemaNumber].name + '\n';
		Session currSession = domainLayer.cinemas [nowCinemaNumber].getSessionById (nowSessionNumber);
		info += domainLayer.films.getFilmByNumber (currSession.filmNumber).name + "\n";
		info += currSession.id + "\n" + currSession.date + "\n" + currSession.time + "\n" + currSession.hall + "\n";
		for(int i = 0; i < 20; ++i)
			for(int j = 0; j < 20; ++j)
				if(currSession.seats[i,j] == true) info += i + " 行 " + j +" 列" + "\n";
		orderInfo.GetComponents<Text> () [0].text = info;
	}

	public void setA(){
		nowCinemaNumber = 0;
	}
	public void setB(){
		nowCinemaNumber = 1;
	}
	public void setC(){
		nowCinemaNumber = 2;
	}

	public void reload() {
		SceneManager.LoadScene (0);
	}
}
